﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTech.Models;
using DTech.Core.Builders;
using TreeViewHelper.Mvc.Demo.Models;

namespace TreeViewHelper.Mvc.Demo.Controllers
{
    public class ProductCategoryController : Controller
    {
        private readonly IHierarchicalDataBuilder<ProductCategoryLevel> productCategoryLevelsBuilder;

        public ProductCategoryController()
        {
            productCategoryLevelsBuilder = new HierarchicalDataBuilder<ProductCategoryLevel>();
        }

        //
        // GET: /ProductCategory/
        public ActionResult Index()
        {
            List<ProductCategoryLevel> categoryLevels = ProductCategoryLevel.GetAll();
            List<String> selectedCategoryLevels = new List<string>();
            selectedCategoryLevels.Add("7");
            //selectedCategoryLevels.Add("4");
            HierarchicalDataModel dataModel = productCategoryLevelsBuilder.CreateHierarchicalDataModel(categoryLevels, false, "/ProductCategory/Details/[id]", "[id]", selectedCategoryLevels, "HierarchyNodes");
            return View(dataModel);
        }

        [HttpPost]
        public ActionResult Index(HierarchicalDataModel dataModel)
        {
            List<string> selectedProductCategoryLevelIDs = productCategoryLevelsBuilder.GetSelectedNodeIDs(dataModel.HierarchyNodes);

            List<ProductCategoryLevel> productCategoryLevels = new List<ProductCategoryLevel>();
            foreach (string selectedProductCategoryLevelID in selectedProductCategoryLevelIDs)
            {
                ProductCategoryLevel categoryLevel = ProductCategoryLevel.GetAll().Where(m => m.ProductCategoryLevelId.Equals(Convert.ToInt32(selectedProductCategoryLevelID))).First();
                productCategoryLevels.Add(categoryLevel);
            }

            return View("Selection" ,productCategoryLevels);
        }

        public ActionResult Details(int id)
        {
            ProductCategoryLevel categoryLevel = ProductCategoryLevel.GetAll().Where(m => m.ProductCategoryLevelId.Equals(id)).First();
            return View(categoryLevel);
        }

    }
}
